package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.*;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.*;
import com.baomidou.ant.vo.response.ResponseGetProcessInfoVo;
import com.baomidou.ant.vo.response.ResponseListProcessVo;
import com.baomidou.ant.vo.response.ResponseProcessNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/process-info")
public class ProcessInfoController {
    @Autowired
    private IProcessInfoService processInfoService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IMachineInfoService machineInfoService;
    @Autowired
    private IWorkOrderService workOrderService;
    @Autowired
    private IPinLibraryService pinLibraryService;


    //下拉框显示工序接口
    @PostMapping("/listProcess")
    public Result listProcess(@RequestBody RequestDtoVo requestDtoVo){
        List<ResponseProcessNameVo> responseProcessNameVo=processInfoService.getProcess(requestDtoVo);
        return Result.getSuccess().setData(responseProcessNameVo);
    }

    @PostMapping("/listProcessOfPin")
    public Result listProcessOfPin(@RequestBody RequestDtoVo requestDtoVo){
        PinLibrary pinLibrary=pinLibraryService.getById(requestDtoVo.getPinId());
        requestDtoVo.setDeptId(pinLibrary.getDeptId());
        List<ResponseProcessNameVo> responseProcessNameVo=processInfoService.getProcess(requestDtoVo);
        return Result.getSuccess().setData(responseProcessNameVo);
    }

    @PostMapping("/listProcessOfUserEdit")
    public Result listProcessOfUserEdit(@RequestBody RequestDtoVo requestDtoVo){
        List<ResponseProcessNameVo> responseProcessNameVo=processInfoService.getProcessOfUserEdit(requestDtoVo);
        return Result.getSuccess().setData(responseProcessNameVo);
    }


    //工序管理展示接口
    @PostMapping("/list")
    public Result list(@RequestBody RequestListProcessVo requestListProcessVo){
        IPage<ResponseListProcessVo> processInfo=processInfoService.listProcess(requestListProcessVo);
        return Result.getSuccess().setData(processInfo);
    }


    //工序信息回显接口
    @GetMapping("/getProcessInfoById/{id}")
    public Result getProcessInfoById(@PathVariable Integer id){
        ProcessInfo processInfo=processInfoService.getById(id);
        return Result.getSuccess().setData(processInfo);
    }

    //工序详情接口
    @GetMapping("/processInfo/{id}")
    public Result orderInfo(@PathVariable Integer id){
        ProcessInfo processInfo=processInfoService.getById(id);
        return Result.getSuccess().setData(processInfo);
    }

    //工序工作中心回显接口
    @PostMapping("/postWorkCenter")
    public Result postWorkCenter(@RequestBody RequestWorkCenterVo requestWorkCenterVo){
        WorkOrder workOrder=workOrderService.queryByOrderNum(requestWorkCenterVo.getOrderNum());
        //得到workCenter
        String workCenter=null;
        if(workOrder.getNowProcess()!=null&&workOrder.getNowProcess()!=0){
            workCenter=processInfoService.getById(workOrder.getNowProcess()).getWorkCenter();
        }
        //如果当前工序已经完成
        if(StringUtils.isBlank(workCenter)){
            workCenter="(未知...)";
            return Result.getSuccess().setData(workCenter);
        }
        return Result.getSuccess().setData(workCenter);
    }

    //获取所需机器信息
    @GetMapping("/getMachine/{id}")
    public Result getProcess(@PathVariable Integer id){
        ProcessInfo processInfo=processInfoService.getById(id);
        //得到工序id集
        int[] machineIds= MyStringUtil.toInt(processInfo.getMachine());
        List<String> machineList=new ArrayList<>();
        for(int i:machineIds){
            if(i==0){
                continue;
            }
            MachineInfo machineInfo=machineInfoService.getById(i);
            machineList.add(machineInfo.getMachineModel());
        }
        StringBuilder result= new StringBuilder();
        for(String str:machineList){
            result.append(str).append(";");
        }
        return Result.getSuccess().setData(result.toString());
    }

    //保存编辑接口
    @PostMapping("/update")
    public Result update(@RequestBody RequestUpdateProcessVo requestUpdateProcessVo){
        ProcessInfo processInfo=processInfoService.getById(requestUpdateProcessVo.getId());
        BeanUtils.copyProperties(requestUpdateProcessVo,processInfo);
        if(processInfoService.updateById(processInfo)){
            return Result.getSuccess().setMsg("保存成功");
        }else
            return Result.getFailure().setMsg("保存失败");
    }

    //删除工单接口
    @PostMapping("/delete")
    public Result delete(@RequestBody RequestDeleteVo requestDeleteVo){
        SysUser user=userService.getById(requestDeleteVo.getUserId());
        //如果不是管理员
        if (!Objects.equals(user.getType(), "1")){
            return Result.getFailure().setMsg("您不是超级管理员，无权删除工序");
        }
        //如果是管理员
        if(processInfoService.removeById(requestDeleteVo.getGoalId())){
            return Result.getSuccess().setMsg("删除成功");
        }else{
            return Result.getFailure().setMsg("删除失败");
        }
    }

    //新增工序接口
    @PostMapping("/save")
    public Result save(@RequestBody RequestSaveProcessVo requestSaveProcessVo){
        SysUser sysUser=userService.getById(requestSaveProcessVo.getUserId());
        if(Objects.equals(sysUser.getType(), "2")){
            return Result.getFailure().setMsg("权限不足!");
        }
        ProcessInfo processInfo=new ProcessInfo();
        BeanUtils.copyProperties(requestSaveProcessVo,processInfo);
        processInfo.setConsume(Objects.equals(requestSaveProcessVo.getConsume(), "1"));
        if(processInfoService.save(processInfo)) {
            return Result.getSuccess().setMsg("新增成功!");
        }else
            return Result.getFailure().setMsg("新增失败.");
    }

    //下拉框获取工序
    @PostMapping("/getProcessInfo")
    public Result getProcessInfo(@RequestBody RequestDtoVo vo){
        List<ResponseGetProcessInfoVo> infoList=new ArrayList<>();
        QueryWrapper<ProcessInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("process_dept",vo.getDeptId());
        List<ProcessInfo> processList=processInfoService.list(queryWrapper);
        for(ProcessInfo processInfo:processList){
            ResponseGetProcessInfoVo responseGetProcessInfoVo=new ResponseGetProcessInfoVo();
            String info=processInfo.getProcessName()+"  "+processInfo.getProcessCode();
            responseGetProcessInfoVo.setProcessName(info);
            responseGetProcessInfoVo.setId(processInfo.getId());
            infoList.add(responseGetProcessInfoVo);
        }
        return Result.getSuccess().setData(infoList);
    }


    @PostMapping("/getProcessName")
    public Result getProcessName(@RequestBody RequestGetProcessNameVo vo){
        int[] ids =MyStringUtil.toInt(vo.getIds());
        StringBuilder processName= new StringBuilder();
        for(int i:ids){
            ProcessInfo processInfo=processInfoService.getById(i);
            processName.append(processInfo.getProcessName()).append(";");
        }
        return Result.getSuccess().setData(processName.toString());
    }
}
